package com.czy.booktest02.demos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czy.booktest02.demos.common.dtos.ResponseResult;
import com.czy.booktest02.demos.common.enums.AppHttpCodeEnum;
import com.czy.booktest02.demos.common.redis.CacheService;
import com.czy.booktest02.demos.common.redis.RedisKey;
import com.czy.booktest02.demos.exception.GlobalException;
import com.czy.booktest02.demos.mapper.BookCategoryMapper;
import com.czy.booktest02.demos.mapper.BookFileMapper;
import com.czy.booktest02.demos.mapper.PublisherMapper;
import com.czy.booktest02.demos.pojo.dto.*;
import com.czy.booktest02.demos.pojo.po.*;
import com.czy.booktest02.demos.mapper.BookMapper;
import com.czy.booktest02.demos.pojo.vo.BookVO;
import com.czy.booktest02.demos.service.BookService;
import com.czy.booktest02.demos.utils.BaseUtils;
import com.czy.booktest02.demos.utils.Code;
import com.czy.booktest02.demos.utils.R;
import com.czy.booktest02.demos.utils.TimeUtil;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.czy.booktest02.demos.constant.ResultStatus.HTTPSERVER_SYSTEM_EXCEPTION;

/**
 *
 **/
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private PublisherMapper publisherMapper;
    @Autowired
    private BookCategoryMapper bookCategoryMapper;
    @Autowired
    CacheService cacheService;


    @Override
    public ResponseResult addOrUpdateBook(BookDto bookDto) {
        int id = bookDto.getId();
        int i=0;
        Map<String,Integer> bookCategoryMap=new HashMap<>();
        for (BookCategory bookCategory : bookMapper.selectAllCategory()) {
            bookCategoryMap.put(bookCategory.getCategoryName(),bookCategory.getId());
        }
        Map<String,Integer> publisherMap=new HashMap<>();
        for (Publisher publisher : bookMapper.selectAllPublisher()) {
            publisherMap.put(publisher.getPublisherName(),publisher.getId());
        }
        Books book = BaseUtils.copyProperties(bookDto, Books.class);

//        if(id>0){
            //更新
            //判断种类
            String category = bookDto.getCategory();
            if(bookCategoryMap.containsKey(category)){
                Integer categoryId = bookCategoryMap.get(category);
                book.setCategoryId(categoryId);
            }else{
                //没有该种类
                int res = bookCategoryMapper.addCategory(bookDto.getCategory());
                if(res>0){
                    BookCategory category1 = bookCategoryMapper.getCategory(bookDto.getCategory());
                    book.setCategoryId(category1.getId());
                }
            }
            //判断出版社
            String publisherName = bookDto.getPublisher();
            if(publisherMap.containsKey(publisherName)){
                Integer publisherId = publisherMap.get(publisherName);
                book.setPublisherId(publisherId);
            }else {
                //没有该出版社

                int i1 = publisherMapper.addPublisher(bookDto.getPublisher());
                if(i1>0){
                    Publisher publisher1 = publisherMapper.getPublisher(bookDto.getPublisher());
                    book.setPublisherId(publisher1.getId());
                }
            }
            if(id>0){
             i = bookMapper.updateBook(book);

            if(i<0){
                return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
            }
        }else{
            //新增
             i = bookMapper.addBook(book);

        }
        if(i<0){
            return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 书城条件分页查询
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Override
    public Page<Books> selectPage(Page<Books> page, QueryWrapper<Books> wrapper) {
        return bookMapper.selectPage(page,wrapper);
    }



    /**
     * 根据id获取书本信息
     *
     * @param id
     * @return
     */
    @Override
    public Books getOneBook(Integer id) {
        Books book = bookMapper.selectById(id);
        return book;
    }





    @Override
    public ResponseResult categoryAndId() {

        List<BookCategory> bookCategories = bookMapper.selectAllCategory();
        if(bookCategories==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        Map<Integer,String> map=new HashMap<>();
        for (BookCategory bookCategory : bookCategories) {
            map.put(bookCategory.getId(),bookCategory.getCategoryName());
        }
        return ResponseResult.okResult(map);

    }

    @Override
    public ResponseResult publisherAndId() {
        List<Publisher> publishers = bookMapper.selectAllPublisher();
        if(publishers==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        Map<Integer,String> map=new HashMap<>();
        for (Publisher publisher : publishers) {
            map.put(publisher.getId(),publisher.getPublisherName());
        }
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult recommend(BookRASDto bookRASDto) {
        if(bookRASDto.getFlagRecommend()>=0)
            if(bookMapper.isRecommend(bookRASDto)>0)
                return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
    }

    @Override
    public ResponseResult status(BookRASDto bookRASDto) {
        if(bookRASDto.getFlagStatus()>=0)
            if (bookMapper.isStatus(bookRASDto)>0)
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
    }

    @Override
    public ResponseResult deleteBook(String bookName) {

        if(Strings.isNotBlank(bookName))
            if(bookMapper.deleteBook(bookName)>0)
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
    }

    @Override
    public ResponseResult manageCategory(String category) {
        Map<String,Integer> bookCategoryMap=new HashMap<>();
        for (BookCategory bookCategory : bookMapper.selectAllCategory()) {
            bookCategoryMap.put(bookCategory.getCategoryName(),bookCategory.getId());
        }
        if(bookCategoryMap.containsKey(category)) {
            //删除
            Integer categoryId = bookCategoryMap.get(category);
            //删除该种类
            int i = bookCategoryMapper.deleteCategory(categoryId);
            //删除该类对应书籍
            deleteBooksByCategory(categoryId);
            if(i>0){
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        }else{
            //新增

            int res = bookCategoryMapper.addCategory(category);
            if(res>0)
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
    }

    @Override
    public ResponseResult updateCategory(CategoryUpdateDto categoryUpdateDto) {
        if (bookCategoryMapper.updateCategory(categoryUpdateDto)>0)
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
    }

    @Override
    public ResponseResult managePublisher(String publisherParam) {
        Map<String,Integer> publisherMap=new HashMap<>();
        for (Publisher publisher : bookMapper.selectAllPublisher()) {
            publisherMap.put(publisher.getPublisherName(),publisher.getId());
        }
        if(publisherMap.containsKey(publisherParam)) {
            //删除
            Integer publisherId = publisherMap.get(publisherParam);
            //删除该种类
            int i = publisherMapper.deletePublisher(publisherId);
            //删除该类对应书籍
            deleteBooksByPublisher(publisherId);
            if(i>0){
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        }else{
            //新增

            int i1 = publisherMapper.addPublisher(publisherParam);
            if(i1>0)
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
    }

    @Override
    public ResponseResult updatePublisher(PublisherUpdateDto publisherUpdateDto) {
        if (publisherMapper.updatePublisher(publisherUpdateDto)>0)
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
    }

    @Override
    public ResponseResult showBook() {
        List<BookVO> bookVOS = bookMapper.selectAllBook();
        if (bookVOS!=null){
            return ResponseResult.okResult(bookVOS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
    }

    @Async
    public int deleteBooksByPublisher(Integer publisherId) {
        return bookMapper.deleteBooksByPublisher(publisherId);
    }

    @Async
    public int deleteBooksByCategory(int categoryId){
        return bookMapper.deleteBooks(categoryId);
    }
}
